﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NationalInstruments.TestStand.Interop.API;

namespace CLVisionMES
{
    public class MqttMesClientNCCaller
    {
        IMqttMesClient mQTTCallerInstance = null;

        private bool CheckInstance(out int ErrorCode, out string ErrorText, out bool Occurred, string methodName)
        {
            if (mQTTCallerInstance == null)
            {
                ErrorCode = -1;
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{methodName}: MQTT客户端对象为null！请检查IntMqttClient初始化是否执行成功！";
                Occurred = true;
                return false;
            }
            ErrorCode = 0;
            ErrorText = $"{nameof(MqttMesClientNCCaller)}:{methodName}: Start!";
            Occurred = false;
            return true;
        }

        public bool IntMqttClient(string server, int port, string username, string password, string topicName, string clientId, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            ErrorCode = 0;
            ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(IntMqttClient)}: Start!";
            Occurred = false;
            try
            {
                mQTTCallerInstance = MqttMesClientFactory.CreateMQTTClient();
                if (mQTTCallerInstance == null)
                {
                    ErrorCode = -1;
                    ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(IntMqttClient)}: 创建MQTT客户端对象失败！请检查dll放置路径是否有误！";
                    Occurred = true;
                    return false;
                }
                bool ret = mQTTCallerInstance.IntMqttClient(server, port, username, password, topicName, clientId, out ErrorCode, out ErrorText);
                if (ErrorCode == -1)
                {
                    Occurred = true;
                    return false;
                }
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(IntMqttClient)}: End!";
                return ret;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(IntMqttClient)}: Fail! " + ex.Message;
                Occurred = true;
                return false;
            }
        }

        public bool PublishData(string topicName, string mesData, MQTTnet.Protocol.MqttQualityOfServiceLevel qos, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            if (!CheckInstance(out ErrorCode, out ErrorText, out Occurred, nameof(PublishData)))
                return false;
            try
            {
                bool ret = mQTTCallerInstance.PublishData(topicName, mesData, qos, out ErrorCode, out ErrorText);
                if (ErrorCode == -1)
                {
                    Occurred = true;
                    return false;
                }
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(PublishData)}: End!";
                return ret;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(PublishData)}: Fail! " + ex.Message;
                Occurred = true;
                return false;
            }
        }

        public bool SubscribeTopic(out int ErrorCode, out string ErrorText, out bool Occurred,params string[] topicNames)
        {
            if (!CheckInstance(out ErrorCode, out ErrorText, out Occurred, nameof(SubscribeTopic)))
                return false;
            try
            {
                bool ret = mQTTCallerInstance.SubscribeTopic(out ErrorCode, out ErrorText, topicNames);
                if (ErrorCode == -1)
                {
                    Occurred = true;
                    return false;
                }
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(SubscribeTopic)}: End!";
                return ret;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(SubscribeTopic)}: Fail! " + ex.Message;
                Occurred = true;
                return false;
            }
        }

        public bool UnsubscribeTopic( out int ErrorCode, out string ErrorText, out bool Occurred, params string[] topicNames)
        {
            if (!CheckInstance(out ErrorCode, out ErrorText, out Occurred, nameof(UnsubscribeTopic)))
                return false;
            try
            {
                bool ret = mQTTCallerInstance.UnsubscribeTopic(out ErrorCode, out ErrorText, topicNames);
                if (ErrorCode == -1)
                {
                    Occurred = true;
                    return false;
                }
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(UnsubscribeTopic)}: End!";
                return ret;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(UnsubscribeTopic)}: Fail! " + ex.Message;
                Occurred = true;
                return false;
            }
        }

        public bool DisconnectMqttClient(out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            if (!CheckInstance(out ErrorCode, out ErrorText, out Occurred, nameof(DisconnectMqttClient)))
                return false;
            try
            {
                bool ret = mQTTCallerInstance.DisconnectMqttClient(out ErrorCode, out ErrorText);
                if (ErrorCode == -1)
                {
                    Occurred = true;
                    return false;
                }
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(DisconnectMqttClient)}: End!";
                return ret;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(DisconnectMqttClient)}: Fail! " + ex.Message;
                Occurred = true;
                return false;
            }
        }

        public bool ReadDataFromFile(PropertyObject propertyObject, string filePath, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            if (!CheckInstance(out ErrorCode, out ErrorText, out Occurred, nameof(ReadDataFromFile)))
                return false;
            try
            {
              
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(ReadDataFromFile)}: End!";
                return true;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(ReadDataFromFile)}: Fail! " + ex.Message;
                Occurred = true;
                return false;
            }
        }

        public bool WriteDataToFile(PropertyObject propertyObject, string filePath, out int ErrorCode, out string ErrorText, out bool Occurred)
        {
            if (!CheckInstance(out ErrorCode, out ErrorText, out Occurred, nameof(WriteDataToFile)))
                return false;
            try
            {
              
               
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(WriteDataToFile)}: End!";
                return true;
            }
            catch (Exception ex)
            {
                ErrorCode = -1;
                ErrorText = $"{nameof(MqttMesClientNCCaller)}:{nameof(WriteDataToFile)}: Fail! " + ex.Message;
                Occurred = true;
                return false;
            }
        }
    }
}
